import { Component, OnInit } from '@angular/core';

// 导入storage服务
import { StorageService } from '../../service/storage.service';

@Component({
  selector: 'app-contact-list',
  templateUrl: './contact-list.component.html',
  styleUrls: ['./contact-list.component.css']
})
export class ContactListComponent implements OnInit {

  private _contactList:{
    id:number,
    name:string,
    tel:string,
    email:string,
    tagId:number
  }[];

  private tagList:{id:number,name:string}[];

  // 当前的tagId
  private tagId:number=0;

  constructor(
    private storage:StorageService
  ) {

  }

  ngOnInit() {
    this._contactList=this.storage.contactList;
    this.tagList=this.storage.tagList;
  }

  // 删除联系人
  contactDel(id:number){
    this.storage.contactDel(id);
  }

  get contactList(){
    return this.tagId>0?this._contactList.filter(item=>item.tagId==this.tagId):this._contactList;
  }

  // 根据tag筛选联系人
  // filterByTagHandle(tagId:number):void{
  //   this.tagId=tagId;
  // }
  
  // 普通函数实现getter访问器的作用
  contactListFilter(){
    return this.tagId>0?this._contactList.filter(item=>item.tagId==this.tagId):this._contactList;
  }


}
